from PyQt5.QtWidgets import  QWidget, QDialog, QColorDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QPixmap, QIcon, QIntValidator

from GUI.Window.Dialog.newImage import Ui_Dialog_NewImage


class NewImage(QDialog, Ui_Dialog_NewImage):
    selectedColor = QColor(255, 255, 255)
    selectedColorIndex = 0

    def __init__(self, parent):
        super().__init__(parent)
        self.initUI(parent)

    @staticmethod
    def newIcon(color: QColor):
        pixmap = QPixmap(32, 32)
        pixmap.fill(color)
        return QIcon(pixmap)

    def initUI(self, parent):
        self.setupUi(self)
        self.setWindowTitle('Photo Sprite Little')
        # self.setWindowFlags(Qt.FramelessWindowHint)
        # 定位
        self.resize(600, 400)
        pos = parent.geometry()
        self.move((pos.width() - self.width())/2, (pos.height() - self.height())/2)
        # 信号
        self.DialogButtonBox.accepted.connect(self.accept)  # 确定
        self.DialogButtonBox.rejected.connect(self.reject)  # 取消
        # 项目
        self.ComboBox_Color.addItem(self.newIcon(QColor(255, 255, 255)), "白色")
        self.ComboBox_Color.addItem(self.newIcon(QColor(0, 0, 0)), "黑色")
        self.ComboBox_Color.addItem(self.newIcon(QColor(150, 150, 150)), "灰色")
        self.ComboBox_Color.addItem("选择颜色")
        self.ComboBox_Color.currentIndexChanged.connect(self.chooseColorSlot)
        self.selectedColorIndex = 3
        # 文本编辑器输入显示
        validator = QIntValidator(16, 2000)
        self.LineEdit_Width.setValidator(validator)
        self.LineEdit_Height.setValidator(validator)

    def chooseColorSlot(self, index):
        if index == self.selectedColorIndex:
            colorWindow = QColorDialog(self.selectedColor)
            color = colorWindow.getColor()
            if color.isValid():
                self.selectedColor = color
                self.ComboBox_Color.setItemIcon(self.selectedColorIndex, self.newIcon(color))
        else:
            if index == 0:
                self.selectedColor = QColor(255, 255, 255)
            elif index == 1:
                    self.selectedColor = QColor(0, 0, 0)
            elif index == 2:
                    self.selectedColor = QColor(150, 150, 150)

